'''
Created on 22/06/2009

@author: nick
'''
import sys
import mlp
from traindata import *
from numpy import *


if __name__ == '__main__':
    print "Inialising Artificial Neural Network...\n"
    #get training data path
    print "Checking: {0} arguments found\n".format(len(sys.argv))
    filepath = None
    if len(sys.argv) > 1:
        print "Argument 2 will be used as the training data source\n"
        filepath = sys.argv[1]
    else:
        filepath = raw_input("Please enter the file path for the training data:\n")
    
    #get training data
    if not filepath:
        filepath = "analysis4_plain.txt"#this file shoudl always exist, for our purposes
    inputdict = traindata.getdictionary(filepath)
    
    training_data = array(traindata.gettraindata())
    #create the MLP
    pred = mlp.mlp(training_data[:,0:4], training_data[:,4:13], 50, outtype='linear')
    #train the MLP
    testpath = raw_input("Please enter the file path for the test data, or enter nothing (\"\") to end the application\n")
    testdata = array(traindata.gettestdata(testpath))
    pred.earlystopping(training_data[:,0:4], training_data[:,4:13], testdata[:,0:4], testdata[:,4:13], 0.1)
    
    usertest = raw_input("Please enter the test parameters separated by spaces")
    usertest = usertest.split(" ")
    test = [[traindata.worddict.index(usertest[0]), traindata.worddict.index(usertest[1]), ord(usertest[2]), ord(usertest[3]), -1], [0, 0, 0, 0, -1]]
    test = array(test)
    userout = pred.mlpfwd(test)

    usernum = 0
    for i in userout[0]:
        print "Usernum is {0} and i is {1}".format(usernum, i)
        usernum = usernum + int(i)
        usernum << 1

    '''
    pred.mlptrain(training_data[:,0:4], training_data[:,4:13], 0.5, 1500)
    print "---MLP ready and trained---\n"
    #request test data path until "exit" or "x" are typed.
    testpath = raw_input("Please enter the file path for the test data, or enter nothing (\"\") to end the application\n")
    while len(testpath) > 0:
        #generate test data from the file
        testdata = array(traindata.gettestdata(testpath))
        total = 0
        correct = 0
        for row in testdata:
            total += 1
            pred_result = pred.mlpfwd(row[0:4])
            if pred_result == row[4:13]:
                print "Word successfully predicted\n"
                correct += 1
            else:
                print "Wrong word"
        
        #print results
        print "Total of {0]% correct ({1} out of {2})".format(correct/total*100.0, correct, total)
        testpath = raw_input("Please enter the file path for the test data, or enter nothing (\"\") to end the application\n")
'''
